<?php
class JobsController extends AppController {
	var $helpers = array('Html','Javascript');
	var $name = 'Jobs';
	//var $uses = array('User', 'Post');
	function search(){
		$options = array(
        	'limit' => 4,
		);
		$this->layout="default1";
		$this->paginate = $options;
		if(isset($_GET['query'])){
			$cond = array('Job.name like' => '%'.$_GET['query'].'%');
			//$this->set('jobs',$this->paginate("Job", $cond));
			$this->set('jobs',$this->paginate('Job',$cond));
			//debug($this->paginate('Job',$cond));
			if(!$this->Session->check('User')){
				//debug('aaaa'.$this->Session->check('User'));
				$this->set('isadmin',false);
			}else{
				$this->set('isadmin',true);
			}
		}
	}
	function deleteall(){
		if(isset($this->data)){
			debug($this->data);
			//if(isset($this->data['del'])){
				//foreach ($this->data['del'] as $id){
					//if($id!=0)
					//$this->delete($id);
				//}
			//}
		}
	}
	function index() {
		$options = array(
        'limit' => 3,
		);
		$this->paginate = $options;
		if(isset($_GET['category'])){
			$cond = array('Category.id' => $_GET['category']);
			//$this->set('jobs',$this->paginate("Job", $cond));
			$this->set('jobs',$this->paginate($cond));
			//debug('aaaa'.$this->Session->check('User'));
			if(!$this->Session->check('User')){
				//debug('aaaa'.$this->Session->check('User'));
				$this->layout="default1";
				$this->set('isadmin',false);
			}else{
				$this->set('isadmin',true);
			}
			return;
		}
		$this->Job->recursive = 0;
		//$this->loadModel('User');
		//debug($this->User->find('all'));
		$this->set('jobs', $this->paginate('Job'));
		//debug($this->paginate());
		if(!$this->Session->check('User')){
			//debug('aaaa'.$this->Session->check('User'));
			$this->layout="default1";
			$this->set('isadmin',false);
		}else{
			$this->set('isadmin',true);
		}
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid job', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('job', $this->Job->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			//debug($this->data);
			$this->Job->create();
			if ($this->Job->save($this->data)) {
				$this->Session->setFlash(__('The job has been saved', true));
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
			}
		}
		$newVar = $this->Job->Category->find('list');
		$users = $this->Job->User->find('list');
		//debug(compact('categories', 'newVar'));
		$this->set(compact('newVar', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid job', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Job->save($this->data)) {
				$this->Session->setFlash(__('The job has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Job->read(null, $id);
		}
		$categories = $this->Job->Category->find('list');
		$users = $this->Job->User->find('list');
		$this->set(compact('categories', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for job', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Job->delete($id)) {
			$this->Session->setFlash(__('Job deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Job was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
